" Specify a directory for plugins
" - For Neovim: stdpath('data') . '/plugged'
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')

" basic
"Plug 'scrooloose/nerdtree'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'junegunn/fzf'
Plug 'junegunn/fzf.vim'
Plug 'junegunn/vim-easy-align'
Plug 'majutsushi/tagbar'

"Plug 'vim-airline/vim-airline'
" more syntax highlight
" it's too show
Plug 'sheerun/vim-polyglot'
Plug 'lsdr/monokai'
Plug 'ap/vim-css-color'

" Initialize plugin system
call plug#end()

set encoding=utf-8
" tabwidth
filetype indent on
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
" line number
set number
" search highlight
set hlsearch
set ignorecase
" column line
set colorcolumn=80

" nerdtree
"noremap <silent> <F3> :NERDTreeToggle<CR>
"let NERDTreeChDirMode=1
"let NERDTreeShowBookmarks=1
"let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']
"let NERDTreeWinSize=25

" fzf
noremap <C-f> :Ag<CR>
noremap <M-f> :Files<CR>

" truecolor sopport
if has("termguicolors")
    set termguicolors
endif

" set syntax on before setup colorscheme
syntax on
set background=dark
colorscheme monokai

" airline
"if !exists('g:airline_symbols')
"    let g:airline_symbols            = {}
"    let g:airline_extensions         = []
"    let g:airline_left_sep           = '▶'
"    let g:airline_right_sep          = '◀'
"    let g:airline_symbols.crypt      = '🔒'
"    let g:airline_symbols.linenr     = '☰'
"    let g:airline_symbols.maxlinenr  = '㏑'
"    let g:airline_symbols.branch     = '⎇'
"    let g:airline_symbols.paste      = 'ρ'
"    let g:airline_symbols.spell      = 'Ꞩ'
"    let g:airline_symbols.notexists  = 'Ɇ'
"    let g:airline_symbols.whitespace = 'Ξ'
"endif

" easy-align
xmap ga <Plug>(EasyAlign)
" usage: [vip][ga][=|.|:|,|,#|"]

" coc.nvim
" if hidden is not set, TextEdit might fail.
set hidden
" Some servers have issues with backup files, see #649
set nobackup
set nowritebackup
" You will have bad experience for diagnostic messages when it's default 4000.
set updatetime=300
" navigate diagnostics
nmap <silent> <C-p> <Plug>(coc-diagnostic-prev)
nmap <silent> <C-n> <Plug>(coc-diagnostic-next)
" remap keys for gotos
nmap <silent> gd <Plug>(coc-definition)
"nmap <silent> gy <Plug>(coc-type-definition)
"nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)
" show documentation in preview window
nnoremap <silent> <C-k> :call <SID>show_documentation()<CR>
function! s:show_documentation()
  if (index(['vim','help'], &filetype) >= 0)
    execute 'h '.expand('<cword>')
  else
    call CocAction('doHover')
  endif
endfunction
" rename current word
nmap <F2> <Plug>(coc-rename)
" format code
nnoremap <F4> :call CocAction('format')<CR>
" fold current buffer
command! -nargs=? Fold :call     CocAction('fold', <f-args>)
" organize import of current buffer
command! -nargs=0 Import   :call     CocAction('runCommand', 'editor.action.organizeImport')
" trigger completion
inoremap <silent><expr> <M-/> coc#refresh()

" golang configure
" https://github.com/golang/tools/blob/master/gopls/doc/vim.md
"autocmd BufWritePre *.go :call CocAction('runCommand', 'editor.action.organizeImport')

" tagbar
nnoremap <silent> <F8> :TagbarToggle<CR>

" copy to system clipboard
if $DISPLAY != ''
	xnoremap Y "+y
endif

" custom theme
"highlight Normal guibg=NONE
"highlight Normal guifg=#F8F8F2 guibg=#272822
"highlight LineNr guibg=NONE
"highlight LineNr ctermfg=11 guifg=#75715E guibg=#3E3D32
highlight! ColorColumn      guibg=#35372c
highlight! LineNr           guibg=#2f2f21
highlight! Visual           guibg=#35372c
highlight! CocErrorSign     guibg=#b9482e
highlight! MatchParen       guifg=#ff8c00
highlight! PmenuSel         guibg=#ff8c00
" depend on vim-polyglot(python-syntax)
highlight! pythonDottedName guifg=#ffbf00
highlight! pythonFunction   guifg=#9acd32
highlight! pythonParameters guifg=#ff8c00
set signcolumn=no
set laststatus=1

highlight!  MatchTab        guibg=#323232
" define a match pattern
match       MatchTab /\t/

" move lines
" 移动当前行到.+1位置 回车 缩进
nnoremap <A-j>    :move .+1<CR>==
nnoremap <A-k>    :move .-2<CR>==
" <Esc> 移动当前行到.+1位置 回车 缩进 回到之前insert模式下位置(gi)
inoremap <A-j>    <Esc>:move .+1<CR>==gi
inoremap <A-k>    <Esc>:move .-2<CR>==gi
" :move '>+1    移动选择的块到'>+1(选择块的最后一行的下一行)
" <CR>          回车,执行指令
" gv            选中之前的选择块
" =             缩进
" gv            选中之前的选择块
vnoremap <A-j>    :move '>+1<CR>gv=gv
vnoremap <A-k>    :move '<-2<CR>gv=gv

" 前一个单词大写
"inoremap <c-u>   <Esc>bvawUgi

" wrap in quotes eg: hello --> "hello"
nnoremap <leader>"  viw<Esc>a"<Esc>bi"<Esc>lel
" `< `>分别表示之前visual模式的第一个字符和最后一个字符
" 注意,和 '< '> 不是一个意思, '< '> 指的是行, `< `> 指字符
vnoremap <leader>"  <Esc>`>a"<Esc>`<i"<Esc>gv

" use jk instead of Esc
" inoremap jk         <Esc>
" inoremap <Esc>      <nop>

"  comment/uncomment 只对单行代码起作用
autocmd FileType c                let b:comment_leader = '// '
autocmd FileType sh,python        let b:comment_leader = '# '
autocmd FileType vim              let b:comment_leader = '" '
function! CommentToggle()
    execute ':silent! s/\([^ ]\)/' . escape(b:comment_leader,'\/') . ' \1/'
    execute ':silent! s/^\( *\)' . escape(b:comment_leader,'\/') . ' \?' . escape(b:comment_leader,'\/') . ' \?/\1/'
endfunction
nnoremap <localleader>\ :call CommentToggle()<CR>

" 一些有用的缩写
iabbrev __main__    if __name__ == '__main__':
iabbrev cmain       int main(int argc, char * argv[])

" 其他的技巧
" v_g_CTRL-A/v_g_CTRL-X 自增/自减
" CTRL-W__/CTRL-W_bar   最高/最宽

autocmd BufWritePost $MYVIMRC source $MYVIMRC
